<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-title">系统登录</div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        label-position="top"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            auto-complete="username"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            auto-complete="current-password"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
            :disabled="loading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '../api/user'

const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 模拟登录请求
    // 实际项目中应该调用login API
    // const { data } = await login(loginForm)
    
    // 模拟成功响应
    setTimeout(() => {
      const token = 'mock-token-' + Date.now()
      localStorage.setItem('token', token)
      localStorage.setItem('userInfo', JSON.stringify({ username: loginForm.username }))
      
      ElMessage.success('登录成功')
      router.push('/home')
      loading.value = false
    }, 1000)
  } catch (error) {
    loading.value = false
    if (error !== false) {
      ElMessage.error('登录失败，请检查网络或稍后重试')
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 30px;
  transition: all 0.3s ease;
}

.login-form-wrapper:hover {
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 30px;
}

.login-form {
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
  border-radius: 6px;
}

.el-input {
  --el-input-bg-color: #f5f7fa;
  transition: all 0.3s ease;
}

.el-input:hover {
  --el-input-bg-color: #ffffff;
}

.el-input__wrapper:focus-within {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.el-button--primary {
  --el-button-bg-color: #667eea;
  --el-button-border-color: #667eea;
  --el-button-hover-bg-color: #5a67d8;
  --el-button-hover-border-color: #5a67d8;
}

@media (max-width: 768px) {
  .login-form-wrapper {
    padding: 20px;
  }
  
  .login-title {
    font-size: 20px;
  }
}
</style>